<?php
namespace Home\Controller;
use Think\Auth;
use Think\Controller;
class ShowController extends CommonController {
    //@todo 加载休闲照页面
    public function aderShow(){
        $id=intval($_GET['id']);
        $name='';
        switch($id){
            case 1;$name='休闲照';break;
            case 2;$name='儿童照';break;
            case 3;$name='婚纱照';break;
            default:$this->error('非法访问');break;
        }
        import('ORG.Util.Auth');//加载类库
        $auth=new Auth();
        if(!$auth->check('Home/Show/UploadFile',$_SESSION['user']['id'])){
            $this->assign('dis','no');
        }else{
            $this->assign('dis','yes');
        }
        $this->assign('id',$id);
        $this->assign('name',$name);
        $this->display();
    }

    public function UploadFile()
    {
        $id=$_GET['id'];
        $timestamp=$_GET['timestamp'];
        $import=new \Think\Upload(C('showPic'));//实例化上传类
        $info=$import->upload();//图片上传操作
        if(!$info){
            $this->ajaxReturn(array('success' =>'error'));
        }
        //生成缩略图
        $thumb= new \Think\Image();
        $pic=$info[0]['savepath'].$info[0]['savename'];
        $uploadSrc=C('showPic')['rootPath'].$pic;
        $thumb->open($uploadSrc);
        $data = date('Ymd',time());
        $thumb->thumb(250, 250)->save(C('showPic')['rootPath'].$info[0]['savepath'].'thumb_'.$info[0]['savename']);
        //存储数据
        $table=D('showpic');
        $data=array();
        $data['pid']=$id;
        $data['timestamp']=$timestamp;
        $data['thumb']=$info[0]['savepath'].'thumb_'.$info[0]['savename'];
        $data['pic']=$pic;
        $data['ac_time']=time();
        $res=$table->add($data);
        if (!$res) {
            $this->ajaxReturn(array('success' =>'error'));
        }
        $this->ajaxReturn(array('success' =>'success'));
    }

    //@todo 查询列表
    public function getListData(){
        $start=$_GET['offset'];
        $limit=$_GET['limit'];
        $id=$_GET['id'];
        $list=D('showpic')->where(array('pid'=>$id))->group('timestamp')->order('id','desc')->select();
        $total=count($list);
        $row=D('showpic')->field('id,thumb,FROM_UNIXTIME(ac_time,"%Y-%m-%d %H:%m:%i") as ac_time,timestamp,pid')
            ->where(array('pid'=>$id))
            ->limit($start,$limit)->group('timestamp')->order('id','desc')->select();
        foreach($row as $key =>$value){
            $row[$key]['thumb_url']=__ROOT__.'/'.'Uploads/Cms/showPic/'.$value['thumb'];
        }
        echo json_encode(array('rows'=>$row,'total'=>$total));
    }
    //@todo 加载页面
    public function detailList(){
        $pid=$_GET['pid'];
        $tempstamp=$_GET['tempstamp'];
        $this->assign('pid',$pid);
        $this->assign('tempstamp',$tempstamp);
        $this->display();
    }
    //@todo 查看详细列表
    public function getDetailList(){
        $start=$_GET['offset'];
        $limit=$_GET['limit'];
        $pid=$_GET['pid'];
        $tempstamp=$_GET['tempstamp'];
        $list=D('showpic')->where(array('pid'=>$pid,'timestamp'=>$tempstamp))->order('id','desc')->select();
        $total=count($list);
        $row=D('showpic')->field('id,thumb,FROM_UNIXTIME(ac_time,"%Y-%m-%d %H:%m:%i") as ac_time,timestamp,pid,pic')->limit($start,$limit)->where(array('pid'=>$pid,'timestamp'=>$tempstamp))->order('id','desc')->select();
        foreach($row as $key =>$value){
            $row[$key]['thumb_url']=__ROOT__.'/'.'Uploads/Cms/showPic/'.$value['thumb'];
            $row[$key]['pic_url']=__ROOT__.'/'.'Uploads/Cms/showPic/'.$value['pic'];
        }
        echo json_encode(array('rows'=>$row,'total'=>$total));
    }

    public function delAction(){
        $pid=$_GET['pid'];
        $timestamp=$_GET['timestamp'];
        $list=D('showpic')->field('thumb,pic,id')->where(array('pid'=>$pid,'timestamp'=>$timestamp))->select();
        foreach($list as $key => $value){
            $thumb='./Uploads/Cms/showPic/'.$value['thumb'];
            $pic='./Uploads/Cms/showPic/'.$value['pic'];
            if(is_file($thumb)&&is_file($pic)){
                unlink($thumb);unlink($pic);
                D('showpic')->delete($value['id']);
            }
        }
        $list=D('showpic')->field('thumb,pic,id')->where(array('pid'=>$pid,'timestamp'=>$timestamp))->select();
        if(!$list){
            $this->ajaxReturn(array('success'=>true,'message'=>'删除成功'));
        }else{
            $this->ajaxReturn(array('success'=>false,'message'=>'删除失败，服务端发生错误'));
        }
    }

    public function delPhoto(){
        $id=$_GET['id'];
        $data=D('showpic')->find($id);
        $thumb='./Uploads/Cms/showPic/'.$data['thumb'];
        $pic='./Uploads/Cms/showPic/'.$data['pic'];

        if(is_file($thumb)&&is_file($pic)){
            unlink($thumb);unlink($pic);
            $_del=D('showpic')->delete($id);
            if(!$_del){
                $this->ajaxReturn(array('success'=>false,'message'=>'删除失败，服务端发生错误'));
            }else{
                $this->ajaxReturn(array('success'=>true,'message'=>'删除成功'));
            }
        }else{
            $this->ajaxReturn(array('success'=>false,'message'=>'删除失败，文件服务端发生错误'));
        }

    }

    public function showBigPhoto(){
        $pid=$_GET['pid'];
        $timestamp=$_GET['timestamp'];
        $list=D('showpic')->field('FROM_UNIXTIME(ac_time,"%Y-%m-%d %H:%m:%i") as ac_time,pic')->where(array('pid'=>$pid,'timestamp'=>$timestamp))->select();
        $row=array();
        foreach($list as $key=>$value){
            $row[$key]['caption']=$value['ac_time'];
            $row[$key]['name']='/Uploads/Cms/showPic/'.$value['pic'];
        }
        $json_string = file_put_contents('data/data.json',json_encode($row));
        $this->display();
    }
}
